package com.uchedao.vehicledetection.view.carlist.fragment;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.uchedao.mybaselibrary.activity.BaseFragment;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.view.carlist.Entity.CarBrand;
import com.uchedao.vehicledetection.view.carlist.SideBar;
import com.uchedao.vehicledetection.view.carlist.adapter.BrandAdapter;
import com.uchedao.vehicledetection.view.carlist.db.CarDbHelper;

import java.util.List;

/**
 * Created by ZhangZhanghua on 2015/9/14 0014.
 */
public class FragmentBrand extends BaseFragment {
    private ListView sortListView;
    private SideBar sideBar;
    private TextView dialog;
    private BrandAdapter adapter;
    private List<CarBrand> SourceDateList;
    private IBrandClickListener listener;

    @Override
    protected String getActivityTag() {
        return "FragmentBrand";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_brand;
    }

    @Override
    protected void initView() {
        sideBar = (SideBar) findViewById(R.id.side_bar);
        dialog = (TextView) findViewById(R.id.dialog);
        sideBar.setTextView(dialog);
        sortListView = (ListView) findViewById(R.id.brand);
    }

    @Override
    protected void initData() {
        SourceDateList = CarDbHelper.getCarBrand(getActivity());
        adapter = new BrandAdapter(getActivity(), SourceDateList);
        sortListView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                int position = adapter.getPositionForSection(s.charAt(0));
                if(position != -1){
                    sortListView.setSelection(position);
                }

            }
        });

        sortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
               int mId =  ((CarBrand) adapter.getItem(position)).getId();
               String mName = ((CarBrand) adapter.getItem(position)).getName();
                if (mId == -1){
                   getActivity().finish();
                }
                listener.onBrandClick(mId, mName);
            }
        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try{
            listener =(IBrandClickListener)activity;
        }catch(ClassCastException e){
            throw new ClassCastException(activity.toString()+"must implement OnArticleSelectedListener");
        }
    }

    public interface IBrandClickListener
    {
        void onBrandClick(int bId , String bName);
    }
}
